﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RIA.ViewModel;
using RIA.ServicesLibrary.Web.Models;

namespace RIA.NTiersApp.Views.CustomerUI
{
    public partial class AddressList : UserControl
    {
        public AddressList ()
        {
            InitializeComponent ();
            this.Loaded += new RoutedEventHandler ( AddressList_Loaded );
        }
        private CustomerDetailsViewModel vm = null;


        
        


        void AddressList_Loaded ( object sender, RoutedEventArgs e )
        {
            vm = this.DataContext as CustomerDetailsViewModel;

            //vm.OnLoadedData += new EventHandler ( vm_OnLoadedData );
            //this.addressDataGrid.RowEditEnded
            //    += new EventHandler<DataGridRowEditEndedEventArgs>
            //        ( addressDataGrid_RowEditEnded );
        }

        #region MyRegion
        //private object addRowBoundItem = null;
        void addressDataGrid_RowEditEnded ( object sender, DataGridRowEditEndedEventArgs e )
        {
            //if ( e.EditAction == DataGridEditAction.Commit )
            //{
            //    if ( e.Row.DataContext == addRowBoundItem )
            //    {
            //        addRowBoundItem = CreateNewCompanyAddress ();
            //        vm.Customer.CustomerAddresses.Add ( addRowBoundItem as CustomerAddress );
            //        addressDataGrid.SelectedItem = addRowBoundItem;
            //        addressDataGrid.CurrentColumn = addressDataGrid.Columns [ 0 ];
            //        addressDataGrid.ScrollIntoView ( addRowBoundItem, addressDataGrid.CurrentColumn );
            //        addressDataGrid.BeginEdit ();
            //    }
            //}
        }

        void vm_OnLoadedData ( object sender, EventArgs e )
        {
            //addRowBoundItem  =  CreateNewCompanyAddress ();
            //vm.Customer.CustomerAddresses.Add ( addRowBoundItem as CustomerAddress );
        }
        
        #endregion

        private void btnAddNew_Click ( object sender, RoutedEventArgs e )
        {
            CustomerAddress ca = vm.CreateNewCompanyAddress ();
            vm.InsertAddress ( ca );
        }
        private void DeleteButton_Click ( object sender, RoutedEventArgs e )
        {
            CustomerAddress ca = ( ( FrameworkElement ) sender ).DataContext as CustomerAddress;
            vm.DeleteAddress ( ca );
        }


       
      
    }
}
